package com.aloda.result;

import com.aloda.constant.ErrorCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 通一返回结果
 * @param <T>
 */
@Data
public class Result<T> implements Serializable {

    /**
     * 返回结果码 1-成功 其他-失败
     */
    private Integer code;

    /**
     * 返回结果信息
     */
    private String msg;

    /**
     * 返回结果数据
     */
    private T data;

    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.code = 1;
        return result;
    }

    public static <T> Result<T> success(String msg) {
        Result<T> result = new Result<T>();
        result.code = 1;
        result.msg = msg;
        return result;
    }

    public static <T> Result<T> success(T object) {
        Result<T> result = new Result<T>();
        result.data = object;
        result.code = 1;
        return result;
    }

    public static <T> Result<T> success(String msg,T object) {
        Result<T> result = new Result<T>();
        result.msg = msg;
        result.data = object;
        result.code = 1;
        return result;
    }

    public static <T> Result<T> error() {
        Result<T> result = new Result<T>();
        result.code = 0;
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result<T> result = new Result<T>();
        result.msg = msg;
        result.code = 0;
        return result;
    }

    public static <T> Result<T> error(Integer code,String msg) {
        Result<T> result = new Result<T>();
        result.code = code;
        result.msg = msg;
        return result;
    }

    public static <T> Result<T> error(ErrorCode errorCode) {
        Result<T> result = new Result<T>();
        result.code = errorCode.getCode();
        result.msg = errorCode.getMessage();
        return result;
    }
}
